package com.zzu.view.fragment;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.king.zxing.CaptureActivity;
import com.king.zxing.Intents;
import com.zzu.R;
import com.zzu.model.bean.JsonBean;
import com.zzu.model.bean.PackageItem;
import com.zzu.presenter.impl.PackagePresenterImpl;
import com.zzu.utils.AppStorage;
import com.zzu.utils.ChangeActivity;
import com.zzu.utils.ToastAndDialogUtils;
import com.zzu.view.activity.TransportAndMapActivity;
import com.zzu.view.view.PackageQueryView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @description:
 * @author: bytedance
 * @date: 2021-05-07 23:35
 * 主页Fragment
 */
public class FragmentZero extends BaseFragment implements View.OnClickListener {

    private final String TAG = FragmentZero.class.getSimpleName();


    @BindView(R.id.tv_zero_search)
    EditText tvZeroSearch;
    //搜索按钮，进行快递订单的搜索
    @BindView(R.id.bt_zero_search)
    Button btZeroSearch;
    @BindView(R.id.im_zero_fahuo)
    ImageView imZeroFahuo;
    @BindView(R.id.tv_zero_fahuo)
    TextView tvZeroFahuo;
    @BindView(R.id.im_zero_paisong)
    ImageView imZeroPaisong;
    @BindView(R.id.tv_zero_paisong)
    TextView tvZeroPaisong;
    @BindView(R.id.im_zero_dabao)
    ImageView imZeroDabao;
    @BindView(R.id.tv_zero_dabao)
    TextView tvZeroDabao;
    @BindView(R.id.im_zero_chaibao)
    ImageView imZeroChaibao;
    @BindView(R.id.tv_zero_chaibao)
    TextView tvZeroChaibao;
    @BindView(R.id.im_zero_chaxun)
    ImageView imZeroChaxun;
    @BindView(R.id.tv_zero_chaxun)
    TextView tvZeroChaxun;
    @BindView(R.id.im_zero_transport)
    ImageView imZeroTransport;
    @BindView(R.id.tv_zero_transport)
    TextView tvZeroTransport;

    //管理员所选择的地址
    private String address = "河南省郑州市中原区";

    @Override
    public View initView() {
        Log.e(TAG, "主页视图被初始化了");
        View view = View.inflate(mContext, R.layout.fragment_zero, null);
        ButterKnife.bind(this,view);

        //初始化监听
        initActions();

        return view;
    }


    private void initActions() {
        btZeroSearch.setOnClickListener(this);
        imZeroFahuo.setOnClickListener(this);
        tvZeroFahuo.setOnClickListener(this);
        imZeroPaisong.setOnClickListener(this);
        tvZeroPaisong.setOnClickListener(this);
        imZeroDabao.setOnClickListener(this);
        tvZeroDabao.setOnClickListener(this);
        imZeroChaibao.setOnClickListener(this);
        tvZeroChaibao.setOnClickListener(this);
        imZeroChaxun.setOnClickListener(this);
        tvZeroChaxun.setOnClickListener(this);
        imZeroTransport.setOnClickListener(this);
        tvZeroTransport.setOnClickListener(this);
    }


    @Override
    public void initData() {
        super.initData();
        Log.e(TAG, "主页数据被初始化了");
    }

    @Override
    public void onClick(View v) {
        Log.i(TAG,"点击操作"+v.getId());
        switch (v.getId()){
            case R.id.bt_zero_search:
                //点击了搜索按钮
                String packageId = tvZeroSearch.getText().toString();
                ChangeActivity.ChangeExpressInfo(mContext,packageId);
                break;
            case R.id.im_zero_fahuo:
            case R.id.tv_zero_fahuo:
                //快递发货
                //唤起摄像头(Zxing)的操作
                startZxing();
                break;
            case R.id.im_zero_paisong:
            case R.id.tv_zero_paisong:
                Toast.makeText(mContext,"请选择当前快递派送位置，默认为当前定位",Toast.LENGTH_SHORT).show();
                showPickerView();
                break;
            case R.id.im_zero_dabao:
            case R.id.tv_zero_dabao:
            case R.id.im_zero_chaibao:
            case R.id.tv_zero_chaibao:
                //先选择位置，在唤起摄像头(Zxing)的操作
                //弹出选择当前的位置
                Toast.makeText(mContext,"请选择当前打包拆包位置，默认为当前定位",Toast.LENGTH_SHORT).show();
                showPickerView();
                break;
            case R.id.im_zero_chaxun:
            case R.id.tv_zero_chaxun:
                //点击了查询选项
                //唤起摄像头(Zxing)的操作
                startZxing();
                break;
            case R.id.tv_zero_transport:
            case R.id.im_zero_transport:
                //点击了快递运输，扫描二维码，然后后台进行数据的写入
                Intent intent = new Intent(mContext, CaptureActivity.class);
                intent.putExtra("address","河南省开封市");
                startActivityForResult(intent,3);
                break;
            default:
                break;
        }
    }

    private void startZxing() {
        Log.i(TAG,"调用申请权限");
        startActivityForResult(new Intent(mContext, CaptureActivity.class),2);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == Activity.RESULT_OK && data!=null){
            if(requestCode==2) {
                //获得到zxing扫出来的条形码的编号然后跳转到详细信息页面
                String result = data.getStringExtra(Intents.Scan.RESULT);
                ChangeActivity.ChangeExpressInfo(mContext,result,address);
            }else if(requestCode==3){
                //获得到zxing扫出来的条形码的编号然后跳转司机运输界面
                String result = data.getStringExtra(Intents.Scan.RESULT);
//                String address = data.getStringExtra("address");
//                Log.i("tag",address);
                //查询是否存在result的包裹
                new PackagePresenterImpl(mContext, new PackageQueryView<PackageItem>() {
                    @Override
                    public void getDates(PackageItem datas) {
                        if(datas == null){
                            ToastAndDialogUtils.showShort(mContext,"没有该包裹信息");
                        }else{
                            //进行跳转
                            Log.i(TAG,datas.toString());
                            Intent intent = new Intent(mContext, TransportAndMapActivity.class);
                            intent.putExtra(TransportInfoFragment.PACKAGE_STATUS,datas.getStatus().toString());
                            intent.putExtra(TransportInfoFragment.PACKAGE_ID,result);//传入
                            Log.i(TAG,"包裹信息"+result+","+datas.getStatus().toString());
                            startActivity(intent);
                        }
                    }

                    @Override
                    public void getNoDate() {
                        //没有包裹信息
                        ToastAndDialogUtils.showShort(mContext,"没有该包裹信息");
                    }
                }).getPackageById(result);
            }
        }
    }

    // 弹出选择器（省市区三级联动）
    private void showPickerView() {

        OptionsPickerView pvOptions = new OptionsPickerBuilder(mContext, new OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                JsonBean jsonBean = AppStorage.options1Items.get(options1);
                //返回的分别是三个级别的选中位置,更改地址信息
                address = AppStorage.options1Items.get(options1).getPickerViewText()+AppStorage.options2Items.get(options1).get(options2)
                        +AppStorage.options3Items.get(options1).get(options2).get(options3);
                Log.i(TAG,"切换到"+AppStorage.options3Items.get(options1).get(options2).get(options3)+"的城市");
                //唤起摄像头
                startZxing();
            }

        })
                .setTitleText("城市选择")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .build();
//        pvOptions.setPicker(options1Items);//一级选择器
//        pvOptions.setPicker(options1Items, options2Items);//二级选择器
        pvOptions.setPicker(AppStorage.options1Items, AppStorage.options2Items, AppStorage.options3Items);//三级选择器
        pvOptions.show();
    }
}
